let code = '';
let lastTime: number | null, nextTime, lastCode: null, nextCode;

// let timer;
let flag = false;

function scanEvent(e: any, cb: any) {
    nextCode = e.which;
    nextTime = new Date().getTime();
    if (!lastTime && !lastCode) {
        code += String.fromCharCode(nextCode);
    }
    if (lastCode != null && lastTime != null && nextTime - lastTime <= 30) {
        code += String.fromCharCode(nextCode);
    } else if (
        lastCode != null &&
        lastTime != null &&
        nextTime - lastTime > 100
    ) {
        code = '';
        code += String.fromCharCode(nextCode);
    }
    if (lastTime && nextTime - lastTime < 30) {
        // 扫码枪
        // do something
        flag = true;
    } else {
        // 键盘
        flag = false;
        // do something
    }
    lastCode = nextCode;
    lastTime = nextTime;
    if (e.which === 13) {
        cb(code);
        code = '';
    }
    return { code: code, flag: flag };
}
export { scanEvent };
